<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PetFindPet;
use App\Models\PetSanProvince;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class PetFindPetController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PetFindPet(), function (Grid $grid) {
            $grid->column('find_id')->sortable();
            $grid->column('title');
            $grid->column('image')->image();
            $grid->column('content');
            $grid->column('uid');
            $grid->column('province_id')->display(function ($province_id){
                if ($province_id){
                    $data = \App\Models\PetSanProvince::find($province_id);
                    if ($data) return $data->name;
                    return '';
                    return $data->name;
                }else{
                    return '';
                }

            });
            $grid->column('city_id')->display(function ($city_id){
                if ($city_id){
                    $data = \App\Models\PetSanCity::find($city_id);
                    if ($data) return $data->name;
                    return '';
                    return $data->name;
                }else{
                    return '';
                }
            });
            $grid->column('area')->display(function ($area){
                if ($area){
                    $data = \App\Models\PetSanArea::find($area);
                    if ($data) return $data->name;
                    return '';
                }else{
                    return '';
                }
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('find_id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PetFindPet(), function (Show $show) {
            $show->field('find_id');
            $show->field('title');
            $show->field('image');
            $show->field('content');
            $show->field('uid');
            $show->field('province_id');
            $show->field('city_id');
            $show->field('area');
            $show->field('latitude');
            $show->field('longitude');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PetFindPet(), function (Form $form) {
            $form->display('find_id');
            $form->text('title');
            $form->image('image');
            $form->editor('content');
            $form->display('uid');
            $form->select('province_id')->options('provinceName')->required()->load('city_id', 'cityName');
            $form->select('city_id')->load('area', 'areaName');
            $form->select('area');
            $form->map('latitude', 'longitude', '选择坐标');
            $form->display('created_at');
            $form->display('updated_at');


        });
    }
}
